<template>
  <div class="home" ref="containerRef"></div>
  <div id="info1">这是球体</div>
  <div id="info2">这是立方体</div>
</template>

<script lang="ts">
import { defineComponent, ref, onMounted } from "vue";
import * as THREE from "three";
import { OrbitControls } from "three/examples/jsm/controls/OrbitControls";

export default defineComponent({
  setup(props) {
    const containerRef = ref<HTMLDivElement>();
    const draw = () => {
      if (!containerRef.value) {
        return;
      }
      let scene = new THREE.Scene();
      /**
       * 创建网格模型
       */
      // var geometry = new THREE.SphereGeometry(60, 40, 40); //创建一个球体几何对象
      let ball = new THREE.SphereGeometry(60, 40, 40); //创建一个立方体几何对象Geometry
      let material1 = new THREE.MeshPhongMaterial({
        color: 0x0000ff,
        specular: 0x4488ee,
        shininess: 12,
      }); //材质对象Material
      let mesh1 = new THREE.Mesh(ball, material1); //网格模型对象Mesh
      mesh1.translateY(120);
      scene.add(mesh1); //网格模型添加到场景中
      let geometry = new THREE.IcosahedronGeometry(50); //创建一个立方体几何对象Geometry
      let material = new THREE.MeshLambertMaterial({
        color: 0x0000ff,
      }); //材质对象Material
      let mesh = new THREE.Mesh(geometry, material); //网格模型对象Mesh
      mesh.position.set(120, 0, 0); //设置mesh3模型对象的xyz坐标为120,0,0
      scene.add(mesh); //网格模型添加到场景中
      function onRender(){
        let canvas = containerRef.value?.querySelector("canvas");
        let canvasWidth = canvas?.clientWidth ?? 0,
          canvasHeight = canvas?.clientHeight ?? 0,
          position1 = new THREE.Vector3(mesh1.position.x,mesh1.position.y,mesh1.position.z),
          position2 = new THREE.Vector3(mesh.position.x,mesh.position.y,mesh.position.z);
          position1 = position1.project(camera);
          position2 = position2.project(camera);
        let info1: HTMLDivElement | null = document.querySelector("#info1");
        info1 &&
          (info1.style.left = `${
            (position1.x * canvasWidth) / 2 + canvasWidth / 2
          }px`);
        info1 &&
          (info1.style.top = `${
            (-position1.y * canvasHeight) / 2 + canvasHeight / 2
          }px`);
        let info2: HTMLDivElement | null = document.querySelector("#info2");
        info2 &&
          (info2.style.left = `${
            (position2.x * canvasWidth) / 2 + canvasWidth / 2
          }px`);
        info2 &&
          (info2.style.top = `${
            (-position2.y * canvasHeight) / 2 + canvasHeight / 2
          }px`);
      }
      /**
       * 光源设置
       */
      //点光源
      let point = new THREE.PointLight(0xffffff);
      point.position.set(400, 200, 300); //点光源位置
      scene.add(point); //点光源添加到场景中
      //环境光
      let ambient = new THREE.AmbientLight(0x444444);
      scene.add(ambient);
      var point2 = new THREE.PointLight(0xffffff);
      point2.position.set(-400, -200, -300); //点光源位置
      scene.add(point2); //点光源添加到场景中
      // console.log(scene)
      // console.log(scene.children)
      /**
       * 相机设置
       */
      let width = window.innerWidth; //窗口宽度
      let height = window.innerHeight; //窗口高度
      let k = width / height; //窗口宽高比
      let s = 200; //三维场景显示范围控制系数，系数越大，显示的范围越大
      //创建相机对象
      let camera = new THREE.OrthographicCamera(-s * k, s * k, s, -s, 1, 1000);
      camera.position.set(200, 300, 200); //设置相机位置
      camera.lookAt(scene.position); //设置相机方向(指向的场景对象)
      /**
       * 创建渲染器对象
       */
      let renderer = new THREE.WebGLRenderer();
      renderer.setSize(width, height - 91); //设置渲染区域尺寸
      renderer.setClearColor(0xb9d3ff, 1); //设置背景颜色
      containerRef.value.appendChild(renderer.domElement); //body元素中插入canvas对象
      //执行渲染操作   指定场景、相机作为参数
      function render() {
        renderer.render(scene, camera); //执行渲染操作
        onRender()
      }
      render();
      let controls = new OrbitControls(camera, renderer.domElement); //创建控件对象
      controls.addEventListener("change", render); //监听鼠标、键盘事件
    };
    onMounted(() => {
      draw();
    });
    return { containerRef };
  },
});
</script>
<style lang="less" scoped>
#info1 {
  position: absolute;

  top: 0;

  width: 100px;

  height: 50px;

  text-align: center;

  z-index: 100;

  display: block;

  padding: 10px;

  background: rgba(255, 255, 255, 0.8);

  line-height: 1;

  border-radius: 5px;
}

#info2 {
  position: absolute;

  top: 0;

  width: 100px;

  height: 50px;

  text-align: center;

  z-index: 100;

  display: block;

  padding: 10px;

  background: rgba(255, 255, 255, 0.8);

  line-height: 1;

  border-radius: 5px;
}
</style>
